package light.mvc.controller.admin;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.admin.User;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.SessionInfo;
import light.mvc.page.base.Status;
import light.mvc.service.admin.UserServiceI;
import light.mvc.utils.GlobalConstant;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	private UserServiceI userService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		return "/admin/user";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(User user,HttpServletRequest request, PageFilter ph) {
		Grid grid = new Grid();
		
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		if(sessionInfo.getRoleId() == 5) {
			List<User> users = new ArrayList<User>();
			users.add(userService.get(sessionInfo.getId()));
			grid.setRows(users);
			grid.setTotal(1L);
		} else {
			grid.setRows(userService.dataGrid(user, ph));
			grid.setTotal(userService.count(user, ph));
		}
		
		return grid;
	}
	
	
	@RequestMapping("/editPwdPage")
	public String editPwdPage(HttpServletRequest request) {
		return "/admin/userEditPwd";
	}
	
	@RequestMapping("/editUserPwd")
	@ResponseBody
	public Json editUserPwd(HttpServletRequest request,String oldPwd, String pwd) {
		String errorMsg = null;
        int errorCode = -1;
        
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
    	try {
			if(userService.editUserPwd(sessionInfo.getId(), oldPwd, pwd)) {
				errorCode = 0;
				errorMsg = "密码修改成功！";
			} else {
				errorMsg = "原密码错误";
			}
			
		} catch (Exception e) {
			errorMsg = "密码修改失败：" + e.getMessage();
		}
		
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		request.setAttribute("sexList", GlobalConstant.SEX_LIST);
		return "/admin/userAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(User user) {
		String errorMsg = null;
        int errorCode = -1;
        Json j = null;
		if( userService.getByUsername(user) != null){
			errorMsg = "用户名已存在!";
			j = new Json(new Status(errorCode,errorMsg),null);
		}else if(userService.getByIdNumber(user.getIdNumber()) != null){
			errorMsg = "该身份证号已被注册!";
			j = new Json(new Status(errorCode,errorMsg),null);
		} else {
			try {
				userService.add(user);
				errorCode = 0;
				errorMsg = "注册成功";
				j = new Json(new Status(errorCode,errorMsg),userService.getByUsername(user));
			} catch (Exception e) {
				errorMsg = e.getMessage();
				j = new Json(new Status(errorCode,errorMsg),null);
			}
			
		}
		
        return j;
	}
	
	@RequestMapping("/get")
	@ResponseBody
	public Json get(HttpServletRequest request) {
		String errorMsg = "用户信息获取失败";
        int errorCode = -1;
        
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
        Integer id = sessionInfo.getId();
		User user = userService.get(id);
		if(user != null) {
			errorMsg = "用户信息获取成功！";
			errorCode = 0;
		}
        
		Json j = new Json(new Status(errorCode, errorMsg),user);
		return j;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Integer id) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			userService.delete(id);
			errorMsg = "删除成功！";
			errorCode = 0;
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Integer id) {
		User user = userService.get(id);
		request.setAttribute("user", user);
		request.setAttribute("sexList", GlobalConstant.SEX_LIST);
		return "/admin/userEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(HttpServletRequest request,User user) {
		String errorMsg = null;
        int errorCode = -1;
        
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
        Integer id = sessionInfo.getId();
        user.setId(id);
		try {
			userService.edit(user);
			errorCode = 0;
			errorMsg = "编辑成功！";
		} catch (ServiceException e) {
			errorMsg = "编辑失败:" + e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/register")
	@ResponseBody
	public Json register(User user) {
		String errorMsg = null;
        int errorCode = -1;
        Json j = null;
		if( userService.getByUsername(user) != null){
			errorMsg = "用户名已存在!";
			j = new Json(new Status(errorCode,errorMsg),null);
		}else if(userService.getByIdNumber(user.getIdNumber()) != null){
			errorMsg = "该身份证号已被注册!";
			j = new Json(new Status(errorCode,errorMsg),null);
		} else {
			user.setRoleId(5);
			try {
				userService.add(user);
				errorCode = 0;
				errorMsg = "注册成功";
				j = new Json(new Status(errorCode,errorMsg),userService.getByUsername(user));
			} catch (Exception e) {
				errorMsg = e.getMessage();
				j = new Json(new Status(errorCode,errorMsg),null);
			}
			
		}
		
        return j;
	}

}
